package com.student.user.service.impl;

import com.student.common.entity.User;
import com.student.user.service.UserService;
import com.student.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * 用户服务实现类
 * 
 * @author student
 * @date 2025
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    @Override
    public User login(String username, String password) {
        User user = userRepository.findByUsername(username);
        if (user != null && user.getPassword().equals(password) && "正常".equals(user.getStatus())) {
            // 更新最后登录时间
            user.setLastLoginTime(LocalDateTime.now());
            userRepository.save(user);
            return user;
        }
        return null;
    }
    
    @Override
    public void logout(String username) {
        // 简单的登出逻辑，实际项目中可能需要处理JWT令牌等
        System.out.println("用户 " + username + " 已登出");
    }
    
    @Override
    public User getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }
    
    @Override
    public boolean register(User user) {
        User existingUser = userRepository.findByUsername(user.getUsername());
        if (existingUser != null) {
            return false;
        }
        
        user.setCreateTime(LocalDateTime.now());
        user.setStatus("正常");
        userRepository.save(user);
        return true;
    }
    
    @Override
    public boolean updateUser(User user) {
        try {
            User existingUser = userRepository.findByUsername(user.getUsername());
            if (existingUser == null) {
                return false;
            }
            
            // 更新用户信息，保留原有密码和创建时间
            existingUser.setRealName(user.getRealName());
            existingUser.setEmail(user.getEmail());
            existingUser.setPhone(user.getPhone());
            existingUser.setUpdateTime(LocalDateTime.now());
            
            userRepository.save(existingUser);
            return true;
        } catch (Exception e) {
            System.err.println("更新用户信息失败: " + e.getMessage());
            return false;
        }
    }
    
    @Override
    public boolean changePassword(String username, String oldPassword, String newPassword) {
        try {
            User user = userRepository.findByUsername(username);
            if (user == null) {
                System.err.println("用户不存在: " + username);
                return false;
            }
            
            // 验证旧密码
            if (!user.getPassword().equals(oldPassword)) {
                System.err.println("当前密码不正确，用户: " + username);
                return false;
            }
            
            // 检查新密码是否与旧密码相同
            if (oldPassword.equals(newPassword)) {
                System.err.println("新密码不能与当前密码相同，用户: " + username);
                return false;
            }
            
            // 更新密码
            user.setPassword(newPassword);
            user.setUpdateTime(LocalDateTime.now());
            userRepository.save(user);
            
            System.out.println("密码修改成功，用户: " + username);
            return true;
        } catch (Exception e) {
            System.err.println("修改密码失败: " + e.getMessage() + "，用户: " + username);
            return false;
        }
    }
    
    @Override
    public boolean createUser(String username, String realName, String role, String relatedId, String email, String phone) {
        try {
            // 检查用户名是否已存在
            User existingUser = userRepository.findByUsername(username);
            if (existingUser != null) {
                System.err.println("用户名已存在: " + username);
                return false;
            }
            
            // 创建新用户
            User user = new User();
            user.setUsername(username);
            user.setPassword("123456"); // 默认密码
            user.setRealName(realName);
            user.setRole(role);
            user.setRelatedId(relatedId);
            user.setEmail(email);
            user.setPhone(phone);
            user.setStatus("正常");
            user.setCreateTime(LocalDateTime.now());
            
            userRepository.save(user);
            System.out.println("用户创建成功: " + username + ", 角色: " + role);
            return true;
        } catch (Exception e) {
            System.err.println("创建用户失败: " + e.getMessage() + ", 用户名: " + username);
            return false;
        }
    }
    
    @Override
    public boolean updateUserInfo(String username, String realName, String email, String phone) {
        try {
            User user = userRepository.findByUsername(username);
            if (user == null) {
                System.err.println("用户不存在: " + username);
                return false;
            }
            
            // 更新用户信息
            user.setRealName(realName);
            user.setEmail(email);
            user.setPhone(phone);
            user.setUpdateTime(LocalDateTime.now());
            
            userRepository.save(user);
            System.out.println("用户信息更新成功: " + username);
            return true;
        } catch (Exception e) {
            System.err.println("更新用户信息失败: " + e.getMessage() + ", 用户名: " + username);
            return false;
        }
    }
    
    @Override
    public boolean deleteUser(String username) {
        try {
            User user = userRepository.findByUsername(username);
            if (user == null) {
                System.err.println("用户不存在: " + username);
                return false;
            }
            
            userRepository.delete(user);
            System.out.println("用户删除成功: " + username);
            return true;
        } catch (Exception e) {
            System.err.println("删除用户失败: " + e.getMessage() + ", 用户名: " + username);
            return false;
        }
    }
}
